package com.bc.commonentity.common;

import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.bc.commonentity.log.LogDetailHelper;
//import com.wordnik.swagger.annotations.ApiModel;
//import com.wordnik.swagger.annotations.ApiModelProperty;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 默认code为成功
 *
 * @author zhaohailong
 */
//@ApiModel
public class JsonResult implements Serializable {

    public JsonResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public JsonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public JsonResult(HttpReturnRnums httpReturnRnums) {
        this(httpReturnRnums, null);
    }

    public JsonResult(HttpReturnRnums httpReturnRnums, Object data) {
        this.code = httpReturnRnums.value();
        this.msg = httpReturnRnums.desc();
        this.data = data;
    }

    public JsonResult(Object data) {
        this.data = data;
    }

    public JsonResult() {
    }

    //	@ApiModelProperty(value = "状态码", notes = "1是成功,其他都是失败")
    private int code = HttpReturnRnums.Success.value();
    //	@ApiModelProperty(value = "状态码描述信息", notes = "例如失败，参数错误")
    private String msg = HttpReturnRnums.Success.desc();
    //	@ApiModelProperty(value = "具体的业务数据", notes = "")
    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        HttpServletRequest request = LogDetailHelper.getRequest();
        String language = CookieUtils.getCookie(request, "local");
        if (StringUtils.isEmpty(language)) {
            //中文
            language = "zh";
        }
        HttpReturnRnums enums = HttpReturnRnums.getEnum(getCode());
        if (null == enums) {
            return "";
        }
        if (language.equals("zh")) {
            return enums.desc();
        }
        return enums.endesc();
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static JsonResult get(int code, String msg, Object data) {
        return new JsonResult(code, msg, data);
    }

    public static JsonResult get(int code, String msg) {
        return JsonResult.get(code, msg, null);
    }

    public static JsonResult get(Object data) {
        return JsonResult.get(1, HttpReturnRnums.Success.desc(), data);
    }

    public boolean isSuccess() {
        return this.code == HttpReturnRnums.Success.value();
    }

}
